package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.SearchTrainBo;
import com.zhuss.bean.unit.product.TrainBean;
import com.zhuss.bean.vo.product.train.DetailLineStrategyVo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.bean.vo.product.train.ListTrainVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.enums.CanSellType;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.OperationStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.util.DateUtil;


/**
 * @description: 车次读
 * @author: zhushanshuo
 * @create: Mon Jan 04 08:55:12 CST 2021
 **/
@Component("trainRead")
public class TrainRead {

	@Autowired
	private TrainDao trainDao;

	@Autowired
	private LineStrategyDao lineStrategyDao;


	public Map<String, Train> listMap() {
		List<Train> list = trainDao.listAll();
		Map<String, Train> map = new HashMap<>();
		for (Train c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}


	public List<Train> listByStrategy(Long lineStrategyId) {
		Date now = DateUtil.toDateOnly(new Date());
		List<Train> result = trainDao.listByStrategy(lineStrategyId,OperationStatus.已开通.key(),CanSellType.售卖.key(), now);
		return result;
	}


	/**
	 * 每日车次
	 * @return
	 */
	public List<Train> listTrain() {
		List<Train> result = trainDao.listAll();
		return result;
	}

	/**
	 * 每日车次
	 * @return
	 */
	public Object listTrainSelect() {
		List<Train> trainList = listTrain();
		List<ListVo> result = new ArrayList<>();
		transferList(trainList, result);
		return result;
	}


	/**
	 * 车次
	 * @return
	 */
	public List<DetailTrainVo> listTrain(List<Long> trainIdList) {
		List<DetailTrainVo> result = trainDao.listById(trainIdList);
		return result;
	}

	/**
	 * 可售卖
	 * 车次
	 * @param lineStrategyId
	 * @return
	 */
	public List<TrainBean> listTrainSell(Long lineStrategyId, List<Long> trainIdList) {
		List<TrainBean> result = trainDao.listByIdSell(lineStrategyId, trainIdList, OperationStatus.已开通.key(), IsEnum.是.key());
		return result;
	}

	/**
	 * 车次
	 * @return
	 */
	public Object listTrainId(List<Long> trainIdList) {
		List<Long> result = trainDao.listIdById(trainIdList);
		return result;
	}


	/**
	 * 根据名字查询车次
	 * @param
	 * @return
	 */
	public List<Train> list(String name) {
		List<Train> result = trainDao.listByName(name);
		return result;
	}

	public List<Train> listNotId(String name, Long trainId) {
		List<Train> result = new ArrayList<Train>();
		List<Train> list = list(name);
		for (Train c : list) {
			if (!c.getTrainId().equals(trainId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 车次
	 * @return
	 */
	public Object listTrainId() {
		List<Train> trainList = trainDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(trainList, result);
		return result;
	}

	private void transferListId(List<Train> trainList, List<Long> result) {
		for (Train item : trainList) {
			result.add(item.getTrainId());
		}
	}

	private void transferList(List<Train> trainList, List<ListVo> result) {
		for (Train item : trainList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getTrainId());
			vo.setLabel(item.getName());
			result.add(vo);
		}

	}

	/**
	 * 车次列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchTrainBo bo) {
		PageVo<ListTrainVo> result = new PageVo<ListTrainVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
		List<ListTrainVo> vo = trainDao.list(bo);
		vo.forEach(train -> {
			DetailLineStrategyVo detail = lineStrategyDao.detail(train.getLineStrategyId());
			train.setBoardName(detail.getBoardNo());
			train.setStrategyType(detail.getStrategyType());
		});

        result.setItems(vo);
        result.setTotal(trainDao.countList(bo));
		return result;
	}

	/**
	 * 车次详情
	 * @param trainId
	 * @return
	 * @throws DzcxException
	 */
	public DetailTrainVo detail(Long trainId) throws DzcxException {
		DetailTrainVo result = trainDao.detail(trainId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}



}
